package com.linx.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.linx.common.core.domain.R;
import com.linx.common.core.validate.AddGroup;
import com.linx.common.core.validate.EditGroup;
import com.linx.common.excel.utils.ExcelUtil;
import com.linx.common.idempotent.annotation.RepeatSubmit;
import com.linx.common.log.annotation.Log;
import com.linx.common.log.enums.BusinessType;
import com.linx.common.mybatis.core.page.PageQuery;
import com.linx.common.mybatis.core.page.TableDataInfo;
import com.linx.common.web.core.BaseController;
import com.linx.system.domain.bo.SysUserGroupBo;
import com.linx.system.domain.vo.SysUserGroupVo;
import com.linx.system.service.ISysUserGroupService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 人员群组
 *
 * @author 泽智科技
 * @date 2024-10-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/user-group")
public class SysUserGroupController extends BaseController {

    private final ISysUserGroupService userGroupService;

    /**
     * 查询人员群组列表
     */
    @SaCheckPermission("system:user-group:list")
    @GetMapping("/list")
    public TableDataInfo<SysUserGroupVo> list(SysUserGroupBo bo, PageQuery pageQuery) {
        return userGroupService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出人员群组列表
     */
    @SaCheckPermission("system:user-group:export")
    @Log(title = "人员群组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysUserGroupBo bo, HttpServletResponse response) {
        List<SysUserGroupVo> list = userGroupService.queryList(bo);
        ExcelUtil.exportExcel(list, "人员群组", SysUserGroupVo.class, response);
    }

    /**
     * 获取人员群组详细信息
     *
     * @param userGroupId 主键
     */
    @SaCheckPermission("system:user-group:query")
    @GetMapping("/{userGroupId}")
    public R<SysUserGroupVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userGroupId) {
        return R.ok(userGroupService.queryById(userGroupId));
    }

    /**
     * 新增人员群组
     */
    @SaCheckPermission("system:user-group:add")
    @Log(title = "人员群组", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysUserGroupBo bo) {
        return toAjax(userGroupService.insertByBo(bo));
    }

    /**
     * 修改人员群组
     */
    @SaCheckPermission("system:user-group:edit")
    @Log(title = "人员群组", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysUserGroupBo bo) {
        userGroupService.updateByBo(bo);

        return R.ok();
    }

    /**
     * 删除人员群组
     *
     * @param userGroupIds 主键串
     */
    @SaCheckPermission("system:user-group:remove")
    @Log(title = "人员群组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userGroupIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userGroupIds) {
        return toAjax(userGroupService.deleteWithValidByIds(List.of(userGroupIds), true));
    }
}
